package com.zhitong.ztblog.mappers;

import com.zhitong.ztblog.entity.Label;
import org.apache.ibatis.annotations.*;

@Mapper
public interface LabelMapper {

    //    查询所有标签
    @Results(id = "labelMap",value = {
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "name",column = "name"),
            @Result(property = "blogNum",column = "id",one = @One(select = "com.zhitong.ztblog.mappers.LabelMapper.findBlogNumByLid"))
    })
    @Select("select * from label")
    public Label[] findAll();

    @Select("select count(*) from label")
    public int findLabelNum();

    //    根据标签id查找标签
    @ResultMap("labelMap")
    @Select("select * from label where id =#{id}")
    public Label findById(int id);

    //根据博客id查询所属标签
    @Select("select * from label where id in (select lid from blog_label where bid=#{bid})")
    public Label[] findByBid(int bid);

    //根据博客id查询所属标签
    @Select("select lid from blog_label where bid=#{bid}")
    public Integer[] findlidByBid(int bid);

    //  根据id查询该标签下的博客
    @Select("select count(*) from blog_label where lid =#{lid}")
    public int findBlogNumByLid(int lid);

    // 随机查询九个标签
    @ResultMap("labelMap")
    @Select("select * from label order by rand() limit 9")
    public Label[] findByRand();

    //根据id删除标签
    @Delete("delete from label where id=#{id}")
    public void delLabelById(int id);

    //添加标签
    @Insert("insert into label value(null,#{name})")
    public void insertLabel(String name);

    //    根据名称查询标签
    @Select("select count(*) from label where name=#{name}")
   public int findLabelNumByName(String Name);

    //    根据名称查询标签
    @Select("select * from label where name=#{name}")
    public Label findLabelByName(String Name);

    //给博客插入标签
    @Insert("insert into blog_label values(#{uid},#{lid})")
    public void bInsertLabelById(int uid,int lid);

    @Select("select name from label where id in (select lid from blog_label where bid=#{bid})")
    public String[] findNamesByBid(int bid);

    @Delete("delete from blog_label where bid=#{bid}")
    public void deleteByBid(int bid);
}
